import { ReactElement } from 'react'
import { Form, DatePicker, Col } from 'antd'
import { ConditionSchema } from '@components/schema/condition'
import moment, { Moment } from 'moment'
import style from './style.module.less'
import { formatBeginDate, formatEndDate } from '@src/assets/js/tool'
const { Item } = Form

function Main (): ReactElement {
  return (
    <Col span={8}>
      <Item label='结案文书签批日期' name='closure_audit_datetime'>
        <DatePicker.RangePicker className={style.full} />
      </Item>
    </Col>
  )
}

export function initFormat (f: { [k: string]: any }, t: { [k: string]: any }): void {
  if (typeof f.closure_audit_datetime_b === 'string' && /^\d+$/.test(f.closure_audit_datetime_b) && typeof f.closure_audit_datetime_e === 'string' && /^\d+$/.test(f.closure_audit_datetime_e)) {
    t.closure_audit_datetime = [moment(parseInt(f.closure_audit_datetime_b) * 1000), moment(parseInt(f.closure_audit_datetime_e) * 1000)]
  }
}

export function changeFormat (d: { [k: string]: any }): void {
  if (d.closure_audit_datetime instanceof Array && d.closure_audit_datetime.length > 0) {
    d.closure_audit_datetime_b = Math.floor((d.closure_audit_datetime[0] as Moment).valueOf() / 1000)
    d.closure_audit_datetime_e = formatEndDate(d.closure_audit_datetime[1] as Moment)
    delete d.closure_audit_datetime
  } else {
    d.closure_audit_datetime_b = undefined
    d.closure_audit_datetime_e = undefined
  }
}

export function getCondition (d: { [k: string]: any }): ConditionSchema[] {
  const condition: ConditionSchema[] = []
  if (typeof d.closure_audit_datetime_b === 'string' && /^\d+$/.test(d.closure_audit_datetime_b)) {
    condition.push({ k: 'closure_audit_datetime', o: '>=', v: formatBeginDate(d.closure_audit_datetime_b) })
  }
  if (typeof d.closure_audit_datetime_e === 'string' && /^\d+$/.test(d.closure_audit_datetime_e)) {
    condition.push({ k: 'closure_audit_datetime', o: '<=', v: formatEndDate(d.closure_audit_datetime_e) })
  }
  return condition
}

export default {
  component: Main,
  initFormat,
  changeFormat,
  getCondition
}
